﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Repositories;

namespace SelfMadeMillionaire.DAL.Repositories
{
    public class PromotionRepository: Repository<Promotion>, IPromotionRepository
    {

        public PromotionRepository(IUnitOfWork uow) : base(uow)
        {
            
        }

        public override Promotion Update(Promotion entity)
        {
            var dbEntity = Get(entity.ID);

            dbEntity.Name = entity.Name;
            dbEntity.Discount = entity.Discount;
            dbEntity.IsPercent = entity.IsPercent;
            dbEntity.PromoCode = entity.PromoCode;
            dbEntity.EffectiveDate = entity.EffectiveDate;
            dbEntity.ExpirationDate = entity.ExpirationDate;
            dbEntity.Product = entity.Product;
            dbEntity.Status = entity.Status;

            Commit();

            return dbEntity;
        }


        public Promotion GetPromoByCode(string promocode)
        {
            var promo = GetAll().FirstOrDefault(x => x.PromoCode.Equals(promocode) && x.ExpirationDate >= DateTime.Now.Date);
            return promo;
        }
    }
}
